# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('openssl' 'libopenssl' 'openssl-devel')
_ver=1.0.2d
# use a pacman compatible version scheme
pkgver=${_ver/[a-z]/.${_ver//[0-9.]/}}
pkgrel=1
pkgdesc='The Open Source toolkit for Secure Sockets Layer and Transport Layer Security'
arch=('i686' 'x86_64')
url='https://www.openssl.org'
license=('custom:BSD')
depends=('zlib')
makedepends=('gcc' 'tar' 'perl' 'zlib-devel')
options=('!makeflags' 'staticlibs')
noextract=(${pkgname}-${_ver}.tar.gz)
source=("https://www.openssl.org/source/${pkgname}-${_ver}.tar.gz"{,.asc}
        'no-rpath.patch'
        'ca-dir.patch'
        'openssl-1.0.1e-cygwin64.patch'
        'openssl-1.0.1e-msys2.patch'
        'openssl-1.0.0-beta5-enginesdir.patch')
md5sums=('38dd619b2e77cbac69b99f52a053d25a'
         'SKIP'
         'dc78d3d06baffc16217519242ce92478'
         '5542c0310f58057fb1b8b199c7c04865'
         '4674951d9fbf4ca4564580a45ed2fe98'
         'edee1c2ea8cda7d1b613ac15ced3a7de'
         '37b6a83811a338c69e78a92c48d96180')
validpgpkeys=('8657ABB260F056B1E5190839D9C4D26D0E604491')

prepare() {
  [ -d ${srcdir}/${pkgname}-${_ver} ] || tar -xzvf ${srcdir}/${pkgname}-${_ver}.tar.gz -C ${srcdir}
  cd ${srcdir}/${pkgname}-${_ver}

  # remove rpath: http://bugs.archlinux.org/task/14367
  patch -p0 -i ${srcdir}/no-rpath.patch
  # set ca dir to /etc/ssl by default
  patch -p0 -i ${srcdir}/ca-dir.patch

  #patch -p1 -i ${srcdir}/openssl-1.0.1e-cygwin64.patch
  patch -p1 -i ${srcdir}/openssl-1.0.1e-msys2.patch
  patch -p1 -i ${srcdir}/openssl-1.0.0-beta5-enginesdir.patch
}

build() {
  cd ${srcdir}/${pkgname}-${_ver}

  if [ "${CARCH}" == 'x86_64' ]; then
    openssltarget='Msys-x86_64'
  elif [ "${CARCH}" == 'i686' ]; then
    openssltarget='Msys'
  fi

  ./Configure \
    --prefix=/usr \
    --openssldir=/usr/ssl \
    --enginesdir=/usr/lib/openssl/engines \
    --libdir=lib \
    shared \
    zlib \
    no-idea \
    no-rc5 \
    no-asm \
    ${openssltarget}

  make depend
  make all
  make INSTALL_PREFIX=${srcdir}/dest MANDIR=/usr/share/man MANSUFFIX=ssl install
}

check() {
  cd ${srcdir}/${pkgname}-${_ver}

  make test
}

package_openssl() {
  depends=('libopenssl' 'zlib')
  optdepends=('ca-certificates' 'perl')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -f ${srcdir}/dest/usr/bin/c_rehash ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/ssl ${pkgdir}/usr/
  install -D -m644 ${srcdir}/${pkgname}-${_ver}/LICENSE ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_libopenssl() {
  depends=('zlib')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/

  mkdir -p ${pkgdir}/usr/lib/openssl
  cp -rf ${srcdir}/dest/usr/lib/engines ${pkgdir}/usr/lib/openssl/
  chmod -R 755 ${pkgdir}/usr/lib/openssl/engines
}

package_openssl-devel() {
  pkgdesc="Openssl headers and libraries"
  groups=('development')
  depends=("libopenssl=${pkgver}" 'zlib-devel')

  mkdir -p ${pkgdir}/usr/lib
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib
}
